/** <!--------------------------------------------------------------------------------------------->
 * @file RevGraphItemMap.h
 *
 * GSvn - A Graphical Client for SVN
 * Copyright (C) 2012  Karl Krach, gsvn@bluespirit.la
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 <!---------------------------------------------------------------------------------------------> */


#ifndef REVGRAPHITEMMAP_H
#define REVGRAPHITEMMAP_H

#include <QWidget>


// -- includes --
#include <QMap>
#include "RevGraphItem.h"
#include <QDebug>
#include "RevGraphItemLink.h"

// --- forward declarations ---
// --- namespaces ---
// --- defines ---


/** <!--------------------------------------------------------------------------------------------->
 * Map to store the revision graphs
 *
 * @author	Karl Krach, <gsvn@bluespirit.la>
 * @since	2012-02-11
 <!---------------------------------------------------------------------------------------------> */
class RevGraphItemMap
{
	/* Con-/Destructors */
public:

	/** <!----------------------------------------------------------------------------------------->
	 * Creates the RevGraphItemMap object
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphItemMap();

	/** <!----------------------------------------------------------------------------------------->
	 * Deletes the RevGraphItemMap object
	 <!-----------------------------------------------------------------------------------------> */
	virtual ~RevGraphItemMap();


	/* Override QMap */
public:

	/** <!----------------------------------------------------------------------------------------->
	 * Returns true when the map contains no elements
	 *
	 * @returns true when map is empty
	 <!-----------------------------------------------------------------------------------------> */
	bool isEmpty() const { return m_aMap.isEmpty(); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns true if the given revision ID is contained by the map
	 *
	 * @returns true, when the element with the given ID is in the map
	 <!-----------------------------------------------------------------------------------------> */
	bool contains( int iRevID ) const { return m_aMap.contains( iRevID ); }

	/** <!----------------------------------------------------------------------------------------->
	 * Inserts the given graph item to the map
	 *
	 * @param pGraphItem (in) item to insert
	 * @returns a link to the inserted element (can be used to determine the neighbours)
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphItemLink insert( RevGraphItem* pGraphItem )
	{
		return RevGraphItemLink( m_aMap.insert( pGraphItem->getRevisionID(), pGraphItem ), &m_aMap );
	}

	/** <!----------------------------------------------------------------------------------------->
	 * Removes the given graph item from the map
	 *
	 * @param pGraphItem (in) item to remove
	 * @returns true, when the item was removed - false otherwise
	 <!-----------------------------------------------------------------------------------------> */
	bool remove( RevGraphItem* pGraphItem )
	{
		if( contains(pGraphItem->getRevisionID()) )
		{
			m_aMap.remove( pGraphItem->getRevisionID() );
			return true;
		}
		return false;
	}

	/** <!----------------------------------------------------------------------------------------->
	 * Removes all items from the list
	 <!-----------------------------------------------------------------------------------------> */
	void removeAll() { m_aMap.clear(); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns the graph item with the given revision ID. Call contains() before!
	 *
	 * @param iRevID (in) ID of element to return
	 * @returns the element requested by given ID
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphItem* get( int iRevID ) const { return m_aMap.value( iRevID ); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns the first element of the map. Call isEmpty() before!
	 *
	 * @returns the first element
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphItem* getFirst() const { return m_aMap.begin().value(); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns a list of all elements
	 *
	 * @returns a list of all elements
	 <!-----------------------------------------------------------------------------------------> */
	QList<RevGraphItem*> getAll() const { return m_aMap.values(); }


	/* Members */
private:

	/** <!----------------------------------------------------------------------------------------->
	 * Map object which stores the mapping
	 <!-----------------------------------------------------------------------------------------> */
	QMap< int, RevGraphItem* > m_aMap;
};

#endif // REVGRAPHITEMMAP_H
